package com.gzsxy.sso.common.exception.impl;



import com.gzsxy.sso.common.exception.IBusinessException;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 异常处理类
 * @date 2021/10/12 16:36
 */
public class BusinessException extends RuntimeException implements IBusinessException {

    private static final long serialVersionUID = -2669670388416077888L;
    private int exceptionCode;
    //Throwable是Error和Exception的父类定义所有可以作为异常被抛出来的类。
    private Throwable realException;


    //重写父类，将异常信息传给父类
    public BusinessException(String message) {
        super(message);
    }

    /**
     * @param msg 异常信息
     * @return
     * @description:
     */
    public static BusinessException getInstance(String msg) {
        return getInstance(msg, 500, (Throwable) null);
    }

    //封装然后异常信息
    public static BusinessException getInstance(String msg, Integer code, Throwable e) {
        BusinessException exception = new BusinessException(msg);
        exception.setExceptionCode(code);
        exception.setRealException(e);
        return exception;
    }

    /**
     * @param msg  异常信息
     * @param code 状态码
     * @return
     * @description:
     */
    public static BusinessException getInstance(String msg, Integer code) {
        return getInstance(msg, code, (Throwable) null);
    }

    /**
     * @param msg 异常信息
     * @param e   具体的异常类型
     * @return
     * @description:
     */
    public static BusinessException getInstance(String msg, Throwable e) {
        return getInstance(msg, 500, e);
    }


    @Override
    public Throwable fillInStackTrace() {
        //为null就取调用父类的异常类型返回给this封装
        return (Throwable) (this.realException == null ? this : super.fillInStackTrace());
    }


    public void setExceptionCode(int exceptionCode) {
        this.exceptionCode = exceptionCode;
    }

    public void setRealException(Throwable realException) {
        this.realException = realException;
    }

    @Override
    public int getExceptionCode() {
        return this.exceptionCode;
    }

    @Override
    public Throwable getRealException() {
        return this.realException;
    }
}
